const app = getApp()
const publicFun = require('/utils/public.js')
import { ssSharePictureList, ssCommentList,ssCurrentDetail,ssCurrentDetailType,getriskContent,commentSub,like,unlike } from '/utils/request'
Page({
  data: {
    userName: '喜欢就评论一下吧',
    correspondList: [
      { url: '/images/detail/icon_car.png', text: '家用小汽车行驶1.81公里所产生的碳排放'},
      { url: '/images/detail/icon_LED.png', text: '连续开灯（节能灯）20个小时所产生的碳排放'},
      { url: '/images/detail/icon_tree.png', text: '每年种树0.5平方米所固定吸收的碳排放'}
    ],
    ssList: [],
    commentDetails: {}
  },
  onLoad(options) {
    this.setData({
      systemInfo: app.globalData.systemInfo,
      ssid: options.ssid,
      sshc: options.sshc,
      ssuserid: options.ssuserid,
      ssList: [],
      commentDetails: {},
      userid: app.globalData.userid
    })
    my.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff',
    })
    my.showLoading();
    this.getSSDetailes();
    this.getSharePictureList();
    this.getAllComment()
  },

  /* 点赞 */
  getLikeState(e) { //是否赞过
    if(app.globalData.userid){
      let data = e.target.dataset;
      let params  = {
        userid: app.globalData.userid,
        dt: publicFun.getTimestamp(),
        wallid: data.ssid
      }
      if(data.state == 1){
        this.unlikeTap(params, data.index);
      }else{
        this.likeTap(params, data.index);
      }
    }else{
      publicFun.jumpLogin();
    }
  },
  async likeTap(params, index) { //点赞
    try{
      const res = await like(params)
      if(res.S == 1){
        this.data.ssList[13] = 1;
        this.data.ssList[7] = Number(this.data.ssList[7])+1;
        this.setData({
          ssList: this.data.ssList
        });
        publicFun.showToast(res.M ? res.M : '点赞成功');
        this.getAllComment();
      }else{
        publicFun.showToast(res.M ? res.M : '加载失败')
      }
    }catch(e){
      publicFun.showToast('失败，请稍后再试')
    }

  },
  async unlikeTap(params, index) { //取消点赞
    try{
      const res = await unlike(params)
      if(res.S == 1){
        this.data.ssList[13] = 0;
        this.data.ssList[7] = Number(this.data.ssList[7])-1;
        this.setData({
          ssList: this.data.ssList
        });
        publicFun.showToast(res.M ? res.M : '取消点赞');
        this.getAllComment();
      }else{
        publicFun.showToast(res.M ? res.M : '加载失败')
      }
      
    }catch(e){
      publicFun.showToast('失败，请稍后再试')
    }
  },

  getCommentVal(e) { //获取框里的内容
    this.setData({
      detailsComment: e.detail.value
    })
  },
  commentUserData(e) { //获取点击的某个要回复用户的信息
    let data = e.target.dataset;
    var ids = {
      parentid: data.parentid,
      parentuserid: data.parentuserid,
      rootid: data.rootid,
      name: data.name
    }
    this.setData({
      showInput: true,
      commentDetails: ids,
      userName: '回复：'+ data.name
    });
  },
  async commentListSub() { //评论单个提交
    if(app.globalData.userid){
      if(this.data.detailsComment){
        try{
          let params = {
            content: this.data.detailsComment,
          }
          my.showLoading();
          const res = await getriskContent(params)
          if(res.S == 1) {
            if(res.D.action == 'PASSED') {
              this.successRiskContentAlone();
            }else{
              my.hideLoading();
              setTimeout(function () {
                publicFun.showToast("您输入的内容涉及敏感词汇，请重新输入。", 2000);
                this.setData({
                  detailsComment: ''
                });
              }, 300)
            }
          }else{
            my.hideLoading();
            publicFun.showToast(res.M ? res.M : '加载失败')
          }
        }catch(e) {
          publicFun.showToast('失败，请稍后再试')
        }
      }else{
        publicFun.showToast("请输入评论内容")
      }
    }else{
      publicFun.jumpLogin();
    }
  },
  async successRiskContentAlone() {
    try{
      let params = {
        userid: app.globalData.userid, //login id
        dt: publicFun.getTimestamp(), //time
        wallid: this.data.ssid, //ss id
        parentid: this.data.commentDetails.parentid ? this.data.commentDetails.parentid : 0, //评论id
        parentuserid: this.data.commentDetails.parentuserid ? this.data.commentDetails.parentuserid : this.data.ssuserid, //被留言id
        Content: this.data.detailsComment, //内容
        rootid: this.data.commentDetails.parentid ? this.data.commentDetails.parentid : 0, // 首次评论 0 
      }
      const res = await commentSub(params)
      if(res.S == 1){
        my.hideLoading();
        setTimeout(function () {
          publicFun.showToast("提交成功", 2000);
        }, 300);
        this.setData({
          commentList: [],
          detailsComment: '',
          commentDetails: {},
          userName: '喜欢就评论一下吧',
        });
        this.getAllComment(this.data.ssid, 1);
      }else{
        my.hideLoading();
      }
    }catch(e) {
      publicFun.showToast('失败，请稍后再试')
    }
  },

  async getSSDetailes() { //获取列表
    try{
      let thisData = this.data;
      let params = {
        id: thisData.ssid,
        lat: app.globalData.location.lat,
        lng: app.globalData.location.lng,
        userid: app.globalData.userid,
        keyclear: thisData.sshc ? thisData.sshc : "",
        type: app.globalData.actiontypeid
      }
      const res = await ssCurrentDetail(params)
      my.hideLoading();
      if(res.S == 1){
        if(res.L.length > 0){
          res.L[2] = publicFun.Base64.decode(res.L[2]);
          res.L[10] = publicFun.Base64.decode(res.L[10]); // 位置
          res.L[11] = publicFun.Base64.decode(res.L[11]); // 描述
          res.L[14] = publicFun.Base64.decode(res.L[14]); // 标题

          res.L[4] = res.L[4].split(" ")[0];

          res.L.forEach((ssItem) => {
            this.data.ssList.push(ssItem);
          });
          this.setData({
            ssList: this.data.ssList
          });
        }
      }else{
        publicFun.showToast(res.M ? res.M : '加载失败')
      }
    }catch(e) {
      my.hideLoading();
      publicFun.showToast('失败，请稍后再试')
    }
  },
  async getSharePictureList() { //ss图片列表
    try{
      let params = {
        id: this.data.ssid,
      }
      const res = await ssSharePictureList(params)
      if(res.S == 1){
        let imgArr = [];
        res.L.forEach((item) => {
          item[2] = item[2].replace(/\/ico/g, '');
          imgArr.push(item);
        });
        this.setData({ imgList: imgArr });
      }else{
        publicFun.showToast(res.M ? res.M : '加载失败')
      }
    }catch(e) {
      publicFun.showToast('失败，请稍后再试')
    }
  },
  async getAllComment() {//获取全部评论
    try{
      let params = {
        id: this.data.ssid
      }
      const res = await ssCommentList(params)
      if(res.S == 1){
        this.setData({
          commentList: res.L
        });
      }else{
        publicFun.showToast(res.M ? res.M : '加载失败')
      }
    }catch(e){
      publicFun.showToast('失败，请稍后再试')
    }
  },
  previewImageTap(e) { //查看大图
    let data = e.currentTarget.dataset;
    let arr = [];
    this.data.imgList.forEach((item) => {
      arr.push(item[2])
    })
    my.previewImage({
      current: data.index,
      enablesavephoto: true,
      urls: arr
    });
  },
});
